package servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingCharsetFilter implements Filter {

	FilterConfig myConf;
	@Override
	public void destroy() {
		// 销毁方法，回收处理
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 转发过滤处理的业务
		//设置所有的请求对象request,response的编码格式
		request.setCharacterEncoding(myConf.getInitParameter("charSet"));
		response.setCharacterEncoding(myConf.getInitParameter("charSet"));
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig conf) throws ServletException {
		// 初始化方法
		//从初始化方法中的参数conf中可以获取web.xml配置文件中的初始化参数
		//init方法在doFilter方法之前执行
		myConf=conf;
	}

}
